home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / netlib / dostat.c < prev    next >
C/C++ Source or Header  |  1994-03-29  |  9KB  |  328 lines

  1. RCS_ID_C="$Id: dostat.c,v 3.1 1994/03/29 12:47:53 ppessi Exp $";
  2. /*
  3.  * dostat.c --- *stat() function common part 
  4.  *
  5.  * Author: ppessi <Pekka.Pessi@hut.fi>
  6.  *
  7.  * This file is part of the AmiTCP/IP Network Support Library.
  8.  *
  9.  * Copyright © 1993,1994 AmiTCP/IP Group, <amitcp-group@hut.fi>
  10.  *             Helsinki University of Technology, Finland.
  11.  *             All rights reserved.
  12.  *
  13.  * Created      : Wed Sep 15 02:20:20 1993 ppessi
  14.  * Last modified: Sun Mar 27 21:30:51 1994 ppessi
  15.  *
  16.  */
  17.  
  18. #include <proto/dos.h>
  19. #include <proto/utility.h>
  20. #include <libraries/usergroup.h>
  21.  
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <errno.h>
  25.  
  26. #include <string.h>
  27. #include <stdlib.h>
  28.  
  29. /* DOS 3.0 and MuFS extensions to file info block */
  30. #include "fibex.h"
  31.  
  32. /*
  33.  * Conversion table from Amiga filetypes to Unix filetypes
  34.  */
  35. const static mode_t ftype[ST_LINKDIR - ST_PIPEFILE + 1] = {
  36.   S_IFIFO,
  37.   S_IFREG,
  38.   S_IFREG,
  39.   S_IFREG,
  40.   S_IFREG,
  41.   S_IFREG,
  42.   S_IFDIR,
  43.   S_IFDIR,
  44.   S_IFLNK,
  45.   S_IFDIR,
  46. };
  47.  
  48. /*
  49.  * Conversion table from Amiga protections to Unix protections
  50.  * rwed -> rwx
  51.  */
  52. const static UBYTE fbits[16] =
  53. {
  54.   00, 02, 01, 03, 02, 02, 03, 03,
  55.   04, 06, 05, 07, 06, 06, 07, 07,
  56. };
  57.  
  58. void __dostat(struct FileInfoBlock *fib,
  59.           struct stat *st)
  60. {
  61.   ULONG pbits = fib->fib_Protection ^ 0xf;
  62.   short fibtype = fib->fib_DirEntryType - ST_PIPEFILE;
  63.   mode_t mode;
  64.  
  65.   if (fibtype < 0)
  66.     fibtype = 0;
  67.   else if (fibtype > ST_LINKDIR - ST_PIPEFILE)
  68.     fibtype = ST_LINKDIR - ST_PIPEFILE;
  69.  
  70.   bzero(st, sizeof(*st));
  71.  
  72.   mode = ftype[fibtype] | (fbits[pbits & 0xf] << 6)
  73.     | (fbits[(pbits >> FIBB_GRP_DELETE) & 0xf] << 3)
  74.       | fbits[(pbits >> FIBB_OTR_DELETE) & 0xf];
  75.  
  76.   if ((pbits & FIBF_PURE) != 0)
  77.     mode |= S_ISVTX;
  78.   if ((pbits & FIBF_SUID) != 0)
  79.     mode |= S_ISUID;
  80.   if ((pbits & FIBF_SGID) != 0)
  81.     mode |= S_ISGID;
  82.  
  83.   st->st_ino = fib->fib_DiskKey;
  84.   st->st_mode = mode;
  85.   st->st_nlink = 1;
  86.   st->st_uid = MU2UG(fib->fib_OwnerUID);
  87.   st->st_gid = MU2UG(fib->fib_OwnerGID);
  88.   st->st_rdev = 0;
  89.   st->st_size = fib->fib_Size;
  90.  
  91.   /* 
  92.    * Calculatory time since Jan 1 1970, UCT 
  93.    * (in reality there are an odd number of leap seconds, 
  94.    * which are not included)
  95.    */
  96.   st->st_atime = st->st_ctime = st->st_mtime =
  97.     60 * ((fib->fib_Date.ds_Days + (8*365+2)) * 24 * 60
  98.       + fib->fib_Date.ds_Minute)
  99.       + fib->fib_Date.ds_Tick / 50;
  100.  
  101.   st->st_blksize = 512;
  102.   st->st_blocks = fib->fib_NumBlocks;
  103.   st->st_dosmode = fib->fib_Protection;
  104.   st->st_type = fib->fib_DirEntryType;
  105.   st->st_comment = fib->fib_Comment;
  106. }
  107.  
  108. /****** net.lib/stat *********************************************************
  109.  
  110.     NAME
  111.         stat, lstat, fstat - get file status
  112.  
  113.     SYNOPSIS
  114.         #include <sys/types.h>
  115.         #include <sys/stat.h>
  116.  
  117.         success = stat(path, buf)
  118.  
  119.         int stat(const char *, struct stat *);
  120.  
  121.         success =  lstat(path, buf);
  122.  
  123.         int lstat(const char *, struct stat *);
  124.  
  125.         success = fstat(fd, buf);
  126.  
  127.         int fstat(int, struct stat *);
  128.  
  129.     DESCRIPTION
  130.         The stat() function obtains information about the file pointed to by
  131.         path. Read, write or execute permission of the named file is not
  132.         required, but all directories listed in the path name leading to the
  133.         file must be seachable.
  134.  
  135.         Lstat() is like stat() except in the case where the named file is a
  136.         symbolic link, in which case lstat() returns information about the
  137.         link, while stat() returns information about the file the link
  138.         references.
  139.  
  140.         The fstat() obtains the same information about an open file known by
  141.         the file descriptor fd, such as would be obtained by an open call.
  142.  
  143.         Buf is a pointer to a stat() structure as defined by <sys/stat.h>
  144.         (shown below) and into which information is placed concerning the
  145.         file.
  146.  
  147.            struct  stat
  148.            {
  149.              dev_t   st_dev;         \* unique device id *\ 
  150.              ino_t   st_ino;         \* inode of file (key block) *\ 
  151.              mode_t  st_mode;        \* Unix style mode *\ 
  152.              ushort  st_nlink;       \* number of links (unimplemented) *\ 
  153.              uid_t   st_uid;         \* owner's user ID *\ 
  154.              gid_t   st_gid;         \* owner's group ID *\ 
  155.              dev_t   st_rdev;        \* special file ID (unimplemented) *\ 
  156.              off_t   st_size;        \* file size *\ 
  157.              time_t  st_atime;       \* Time of last access *\ 
  158.              time_t  st_mtime;       \* Last modification time *\ 
  159.              time_t  st_ctime;       \* Last file status change time *\ 
  160.              long    st_blksize;     \* Size of disk block *\ 
  161.              long    st_blocks;      \* Size in blocks *\ 
  162.              long    st_dosmode;     \* DOS protection bits *\ 
  163.              short   st_type;        \* DOS file type *\ 
  164.              char   *st_comment;     \* DOS file comment *\ 
  165.            };
  166.  
  167.         The time-related fields of struct stat have same contents, time when
  168.         file data last modified.
  169.  
  170.         The status information word st_mode has bits as follows:
  171.  
  172.           #define S_ISUID  0004000    \* set user id on execution *\ 
  173.       #define S_ISGID  0002000    \* set group id on execution *\ 
  174.       #define S_ISVTX  0001000    \* save swapped text even after use *\ 
  175.       #define S_IRUSR  0000400    \* read permission for owner *\ 
  176.       #define S_IWUSR  0000200    \* write permission for owner *\ 
  177.       #define S_IXUSR  0000100    \* execute permission for owner *\ 
  178.       #define S_IRGRP  0000040    \* read permission for group *\ 
  179.       #define S_IWGRP  0000020    \* write permission for group *\ 
  180.       #define S_IXGRP  0000010    \* execute permission for group *\ 
  181.       #define S_IROTH  0000004    \* read permission for other *\ 
  182.       #define S_IWOTH  0000002    \* write permission for other *\ 
  183.       #define S_IXOTH  0000001    \* execute permission for other *\ 
  184.       #define S_IFCHR  0020000    \* character special *\ 
  185.       #define S_IFDIR  0040000    \* directory *\ 
  186.       #define S_IFBLK  0060000    \* block special *\ 
  187.       #define S_IFREG  0100000    \* regular *\ 
  188.       #define S_IFLNK  0120000    \* symbolic link *\ 
  189.       #define S_IFSOCK 0140000    \* socket *\ 
  190.       #define S_IFIFO  0010000    \* named pipe (fifo) *\ 
  191.  
  192.         For a list of access modes, see <sys/stat.h>, access(2) and chmod(2).
  193.  
  194.     RETURN VALUES
  195.         Upon successful completion a value of 0 is returned.  Otherwise, a
  196.         value of -1 is returned and errno is set to indicate the error.
  197.  
  198.     ERRORS
  199.         The functions stat() and lstat() will fail if:
  200.  
  201.         [ENOTDIR]       A component of the path prefix is not a directory.
  202.  
  203.         [ENAMETOOLONG]  A component of a pathname exceeded 255 characters,
  204.                         or an entire path name exceeded 1023 characters.
  205.  
  206.         [ENOENT]        The named file does not exist.
  207.  
  208.         [ELOOP]         Too many symbolic links were encountered in
  209.                         translating the pathname.
  210.  
  211.         [EACCES]        Search permission is denied for a component of the
  212.                         path prefix.
  213.  
  214.         [EFAULT]        Buf or name points to an invalid address.
  215.  
  216.         [EIO]           An I/O error occurred while reading from or writing
  217.                         to the file system.
  218.  
  219.         The function fstat() will fail if:
  220.  
  221.         [EBADF]   fd is not a valid open file descriptor.
  222.  
  223.         [EFAULT]  Buf points to an invalid address.
  224.  
  225.         [EIO]     An I/O error occurred while reading from or writing to the
  226.                   file system.
  227.  
  228.     SEE ALSO
  229.         chmod(),  chown()
  230.  
  231.     BUGS 
  232.         Applying fstat to a socket returns a zero'd buffer.
  233.  
  234. *******************************************************************************
  235. */
  236.  
  237. /****** net.lib/fstat *********************************************************
  238.  
  239.     SEE ALSO
  240.         stat()
  241.  
  242. *******************************************************************************
  243. */
  244.  
  245. /****** net.lib/lstat *********************************************************
  246.  
  247.     SEE ALSO
  248.         stat()
  249.  
  250. *******************************************************************************
  251. */
  252.  
  253. #ifdef DEBUGGING
  254.  
  255. #include <stdio.h>
  256. #include <fcntl.h>
  257.  
  258. void printstat(char *what, struct stat *st)
  259. {
  260.   printf("stat(%s):\n"
  261.      "  st_dev     =%lx\n"
  262.      "  st_ino     =%ld\n"
  263.      "  st_mode    =%o\n"
  264.      "  st_nlink   =%d\n"
  265.      "  st_uid     =%ld\n"
  266.      "  st_gid     =%ld\n"
  267.      "  st_rdev    =%lx\n"
  268.      "  st_size    =%ld\n"
  269.      "  st_atime   =%ld\n"
  270.      "  st_mtime   =%ld\n"
  271.      "  st_ctime   =%ld\n"
  272.      "  st_blksize =%ld\n"
  273.      "  st_blocks  =%ld\n",
  274.      what, 
  275.      st->st_dev,
  276.      st->st_ino,
  277.      st->st_mode,
  278.      st->st_nlink,
  279.      st->st_uid,
  280.      st->st_gid,
  281.      st->st_rdev,
  282.      st->st_size,
  283.      st->st_atime,
  284.      st->st_mtime,
  285.      st->st_ctime,
  286.      st->st_blksize,
  287.      st->st_blocks);
  288. }
  289. void main(int argc, char *argv[])
  290. {
  291.   struct stat st[1];
  292.  
  293.   if (argc > 1) {
  294.     if (stat(argv[1], st) == 0) {
  295.       printstat(argv[1], st);
  296.     } else {
  297.       perror(argv[1]);
  298.     }
  299.   } 
  300.  
  301.   argv++;
  302.   if (argc > 2) {
  303.     if (lstat(argv[1], st) == 0) {
  304.       printstat(argv[1], st);
  305.     } else {
  306.       perror(argv[1]);
  307.     }
  308.   } 
  309.  
  310.   argv++;
  311.   if (argc > 3) {
  312.     int fd = open(argv[1], O_RDONLY, 0);
  313.  
  314.     if (fd > 0) {
  315.       if (fstat(fd, st) == 0) {
  316.     printstat(argv[1], st);
  317.       } else {
  318.     perror("fstat");
  319.       }
  320.     } else {
  321.       perror(argv[1]);
  322.     }
  323.   }
  324. }
  325.  
  326. #endif
  327.  
  328.